//
//  UISliderViewController.m
//  UITest
//
//  Created by wp w on 12-6-9.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UISliderViewController.h"
@implementation UISliderViewController

UILabel *lblSliderValue;
-(void)sliderAction:(id)sender
{
//    //显示浮点值，即，直接显示
//    float stepValue = [(UISlider *)sender value];
//    lblSliderValue.text = [NSString stringWithFormat:@"%f",stepValue];
    
    //显示整数值
    int stepAmount  = 10;
    float stepValue = (abs([(UISlider *)sender value]) / stepAmount) * stepAmount;
    [(UISlider *)sender setValue:stepValue];
    
    lblSliderValue.text = [NSString stringWithFormat:@"%d",(int)stepValue];
}


- (void)viewDidLoad {
	
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //新建一个平常的slider
    CGRect sliderRect1 = CGRectMake(20, 5, 300, 30);
    UISlider *mySlider1 = [[UISlider alloc] initWithFrame:sliderRect1];
    mySlider1.minimumValue = 0;
	mySlider1.maximumValue = 100;
	mySlider1.continuous   = YES;
    [mySlider1 setValue:50.0f];
    //handle value change events
	[mySlider1 addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    
	//新建一个自定义slider
	CGRect sliderRect = CGRectMake(20,50,280,40);
	UISlider *mySlider = [[UISlider alloc] initWithFrame:sliderRect];
	
	mySlider.minimumValue = 0;
	mySlider.maximumValue = 100;
	mySlider.continuous   = YES;
	
	//images
	UIImage *leftTrackImage  = [[UIImage imageNamed:@"left_slider.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
	UIImage *rightTrackImage = [[UIImage imageNamed:@"right_slider.png"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
	UIImage *thumbImage      = [UIImage imageNamed:@"apple_thumb.png"];
	UIImage *minSliderImage  = [UIImage imageNamed:@"apple_min.png"];
	UIImage *maxSliderImage  = [UIImage imageNamed:@"apple_max.png"];
	
	[mySlider setThumbImage:thumbImage forState:UIControlStateNormal];
	[mySlider setMinimumTrackImage:leftTrackImage forState:UIControlStateNormal];
	[mySlider setMaximumTrackImage:rightTrackImage forState:UIControlStateNormal];
	[mySlider setMinimumValueImage:minSliderImage];
	[mySlider setMaximumValueImage:maxSliderImage];
	[mySlider setValue:50.0f];
	
	//handle value change events
	[mySlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
	
	//label to show current value
	CGRect lblRect = CGRectMake(145,100,100,20);
	lblSliderValue = [[UILabel alloc] initWithFrame:lblRect];
	lblSliderValue.backgroundColor = [UIColor clearColor];
	
	lblSliderValue.text = [NSString stringWithFormat:@"%d",(int)mySlider.value];
	
	//add slider to main view
    [self.view addSubview:mySlider1];
	[self.view addSubview:mySlider];	
	[self.view addSubview:lblSliderValue];	
	
	[lblSliderValue release];
	[mySlider release];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
